import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;

public class ImplementImagImageProcessorTest {
	private int tempOfInt = 0xff;

	// private String pathOfBmp1 = "../bmptest/1.bmp";
	private String pathOfRedBmp1 = "../bmptest/saveResult/RedBmp1.bmp";
	private String pathOfGreenBmp1 = "../bmptest/saveResult/GreenBmp1.bmp";
	private String pathOfBlueBmp1 = "../bmptest/saveResult/BlueBmp1.bmp";
	private String pathOfGrayBmp1 = "../bmptest/saveResult/GrayBmp1.bmp";

	private String pathOfGoalRedBmp1 = "../bmptest/goal/1_red_goal.bmp";
	private String pathOfGoalGreenBmp1 = "../bmptest/goal/1_green_goal.bmp";
	private String pathOfGoalBlueBmp1 = "../bmptest/goal/1_blue_goal.bmp";
	private String pathOfGoalGrayBmp1 = "../bmptest/goal/1_gray_goal.bmp";

	// test the width of saveBmp is equal to the width of goalBmp
	@Test
	public void testWidth() {
		int saveBlueWidth = getWidthOfBmp(pathOfBlueBmp1);
		int goalBlueWidth = getWidthOfBmp(pathOfGoalBlueBmp1);

		int saveGrayWidth = getWidthOfBmp(pathOfGrayBmp1);
		int goalGrayWidth = getWidthOfBmp(pathOfGoalGrayBmp1);

		int saveGreenWidth = getWidthOfBmp(pathOfGreenBmp1);
		int goalGreenWidth = getWidthOfBmp(pathOfGoalGreenBmp1);

		int saveRedWidth = getWidthOfBmp(pathOfRedBmp1);
		int goalRedWidth = getWidthOfBmp(pathOfGoalRedBmp1);

		assertEquals(saveBlueWidth, goalBlueWidth);
		assertEquals(saveGrayWidth, goalGrayWidth);
		assertEquals(saveGreenWidth, goalGreenWidth);
		assertEquals(saveRedWidth, goalRedWidth);
	}

	// test the height of saveBmp is equal to the height of goalBmp
	@Test
	public void testHeight() {
		int saveBlueHeight = getHeightOfBmp(pathOfBlueBmp1);
		int goalBlueHeight = getHeightOfBmp(pathOfGoalBlueBmp1);

		int saveGrayHeight = getHeightOfBmp(pathOfGrayBmp1);
		int goalGrayHeight = getHeightOfBmp(pathOfGoalGrayBmp1);

		int saveGreenHeight = getHeightOfBmp(pathOfGreenBmp1);
		int goalGreenHeight = getHeightOfBmp(pathOfGoalGreenBmp1);

		int saveRedHeight = getHeightOfBmp(pathOfRedBmp1);
		int goalRedHeight = getHeightOfBmp(pathOfGoalRedBmp1);

		assertEquals(saveBlueHeight, goalBlueHeight);
		assertEquals(saveGrayHeight, goalGrayHeight);
		assertEquals(saveGreenHeight, goalGreenHeight);
		assertEquals(saveRedHeight, goalRedHeight);
	}

	// test the size of saveBmp is equal to the size of goalBmp
	@Test
	public void testInfo() {
		byte[] saveSizeImage;
		byte[] goalSizeImage;

		saveSizeImage = getSizeImageOfBmp(pathOfBlueBmp1);
		goalSizeImage = getSizeImageOfBmp(pathOfGoalBlueBmp1);
		assertArrayEquals(saveSizeImage, goalSizeImage);

		saveSizeImage = getSizeImageOfBmp(pathOfGrayBmp1);
		goalSizeImage = getSizeImageOfBmp(pathOfGoalGrayBmp1);
		assertArrayEquals(saveSizeImage, goalSizeImage);

		saveSizeImage = getSizeImageOfBmp(pathOfGreenBmp1);
		goalSizeImage = getSizeImageOfBmp(pathOfGoalGreenBmp1);
		assertArrayEquals(saveSizeImage, goalSizeImage);

		saveSizeImage = getSizeImageOfBmp(pathOfRedBmp1);
		goalSizeImage = getSizeImageOfBmp(pathOfGoalRedBmp1);
		assertArrayEquals(saveSizeImage, goalSizeImage);

	}

	// get the width of Image
	public int getWidthOfBmp(String pathname) {
		try {
			File file = new File(pathname);
			@SuppressWarnings("resource")
			FileInputStream fis = new FileInputStream(file);

			byte bmpHeader[] = new byte[54];

			fis.read(bmpHeader, 0, 54);

			int width = (int) ((bmpHeader[21] & tempOfInt) << 24 | (bmpHeader[20] & tempOfInt) << 16
					| (bmpHeader[19] & tempOfInt) << 8 | (bmpHeader[18] & tempOfInt));
			return width;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}

	// get the height of Image
	public int getHeightOfBmp(String pathname) {
		try {
			File file = new File(pathname);
			@SuppressWarnings("resource")
			FileInputStream fis = new FileInputStream(file);

			byte bmpHeader[] = new byte[54];

			fis.read(bmpHeader, 0, 54);

			int height = (int) ((bmpHeader[21] & tempOfInt) << 24 | (bmpHeader[20] & tempOfInt) << 16
					| (bmpHeader[19] & tempOfInt) << 8 | (bmpHeader[18] & tempOfInt));
			return height;
		} catch (Exception e) {
			// TODO: handle exception
			return 0;
		}
	}

	// get the number of bytes occupied by the bitmap of all the pixels
	public byte[] getSizeImageOfBmp(String pathname) {
		try {
			File file = new File(pathname);
			@SuppressWarnings("resource")
			FileInputStream fis = new FileInputStream(file);

			byte bmpHeader[] = new byte[54];
			fis.read(bmpHeader, 0, 54);

			// bmpHeader[34~37]
			int biSizeImage = (int) ((bmpHeader[37] & tempOfInt) << 24 | (bmpHeader[36] & tempOfInt) << 16
					| (bmpHeader[35] & tempOfInt) << 8 | (bmpHeader[34] & tempOfInt));

			// translate into byte[]
			byte allBmpByte[] = new byte[biSizeImage];
			fis.read(allBmpByte, 0, biSizeImage);
			return allBmpByte;
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}

}